{$i-,s-,g+,r-}
{$ifndef modif}{$i modif.inc}{$endif}
Program CannyhoDetektor;
uses {$ifdef Windows}
     WinCrtx,WinDos,
     {$else}
      dos,{$ifdef crtx}crtx,{$else}crt,{$endif}
     {$endif}
     pictures;

var grDriver,grMode,ErrCode: Integer;
    vystup,a:string;
    obr,fbr:Picture;
    i,j:integer;
    MaxP,p:Byte;
    x,y:integer;
    analyse:Boolean;
    MarkedTime:Longint;
    scale,disperze:real;
    dispDefined:boolean;
    PassLProc:PercentLineProc;

Function GetInterval:Longint; assembler;
asm
	MOV	AH,0
        INT	1Ah
        MOV	AX,DX
        MOV	DX,CX
end;


Label 1,10,20;


Function PercentLineProc(l:integer;var p:AbstractPicture):Byte; far;
var i:longint;
begin
 if l mod 8 <>0 then exit;
 if l<=0 then exit;
 i:=(longint(100)*l)div p.y;
 write(#13,i:7,'%');
{ if keypressed then halt(1);}
 PercentLineProc:=0;
end;

Function Percent2LineProc(l:integer;var p:AbstractPicture):Byte; far;
var i:longint;
const lastl:integer=0;
      It:integer=0;
begin
 if l mod 8 <>0 then exit;
 if l<=0 then exit;
 if lastL>=l then inc(It);	{nova iterace}
 lastL:=L;
 i:=(longint(100)*l)div p.y;
 write(#13'it:',It:2,':',i:3,'%');
{ if keypressed then halt(1);}
 Percent2LineProc:=0;
end;

const Title:array[0..21]of char='Canny Edge Detection'#0;
begin
{$ifdef Windows}
ScreenSize.X:=62;
ScreenSize.Y:=10;
WindMax:=ScreenSize;
Move(Title,WindowTitle,Sizeof(Title));
{$endif}

PassLProc.init;
obr.init;
fbr.init;
{$ifndef  Windows}
  DirectVideo:=False;
  nosound;
  {$endif}
writeln(#10#13'<<< CANNY >>> Canny''s edge detector (c)1995 F&TSoft+PiSoft');
vystup:='';
dispDefined:=false;
disperze:=1;
scale:=-1;
if Paramcount=0 then
	begin
1:	write('Enter name of input picture:');readln(a);
        if a='' then goto 1;
	assignpicture(obr,a);
	write('Enter name of output picture:');readln(a);
	vystup:=a;
10:     write('Enter dispersion:');readln(a);
	val(a,disperze,j);
	if (j<>0)or(disperze<=0) then
		     begin
		     writeln('Error:Incorrect number ',a);
		     goto 10;
		     end;
20:     write('Enter scale:');readln(a);
	val(a,scale,j);
	if (j<>0) then
		  begin
		  writeln('Error:Incorrect number ',a);
		  goto 10;
		  end;
	end;

for i:=1 to paramcount do
	begin
	a:=paramStr(i);
	if(a[2]='?')or(a[1]='?') then
		begin
		writeln(#10#13'CANNY file_in file_out [dispersion [scale]] '#10#13+
			      'Supported file formats are OKO,PCX,RAS'#10#10#13+
			      ' if scale is negative or missing ->full canny scale detector'#10#13);
                {$ifdef Windows}InactiveWinCrt;{$endif}
		exit;
		end;

	if a[1]='.' then a:='0'+a;
	if a[1] in ['-','+','0'..'9'] then
			      begin
			      if dispdefined then val(a,scale,j)
					     else val(a,disperze,j);
			      dispdefined:=true;
			      if j<>0 then begin
					   writeln('Error:Incorrect number ',a);
					   {$ifdef Windows}InactiveWinCrt;{$endif}
					   exit;
					   end;
			      end
		else
		  begin
		  if obr.filename=nil then assignpicture(obr,a);
		  vystup:=a;
		  end;

        end;

if obr.filename=nil then
	begin
        writeln('Error:Unknown filename');
{$ifdef Windows}InactiveWinCrt;{$endif}
        exit;
        end;
write(#13' Loading');ClrEol;
loadpicture(obr,'',nil);{} {  loadpicture(fbr);{}
if obr.data=nil then
	begin
        writeln(#10#13'Cannot find input picture:''',obr.filename^,'''');
{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
        end;

markedTime:=GetInterval;

{AlineProc:=PercentLineProc;}
if scale>0 then GausFunc(Obr,gfCannyFast,round(disperze),round(Scale*$80000),0,nil)
	else
	begin			{uplny cannyho detektor - s lokalni aproximaci}
{       AlineProc:=Percent2LineProc;}
	Canny(Obr,fbr,round(Disperze),Scale,nil);
{	AlineProc:=DefaultLineProc;}
	MovePicture(fbr,Obr,nil);
        fbr.Erase;
        end;


fbr.Done;
write(#13' Saving ');ClrEol;
if not(savepicture(obr,vystup,nil)) then
	begin
        writeln(#10#13'Cannot save Output picture:''',obr.filename^,'''');
{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
        end;

writeln(#13' OK     ');
obr.Done;
end.
